package com.adjy.pathbuilders.rendering;

public class DisplayConverter {
	
	private static final float TARGET_WIDTH = 800;
	private static final float TARGET_HEIGHT = 480;
	
	private static float screenRatioW, screenRatioH;
	private static float physicsRatioW, physicsRatioH;
	
	private static float screenWidth = -1, screenHeight = -1;
	private static float srcHeight = -1;
	
	public static void setScreenLengths(float width, float height) {
		if (screenWidth != -1 || screenHeight != -1)
			return;
		
		screenWidth = width;
		screenHeight = height;
		screenRatioW = width / TARGET_WIDTH;
		screenRatioH = height / TARGET_HEIGHT;
	}
	
	public static void setPhysicsSize(float srcWidth, float srcHeight) {
		if (screenWidth == -1 || screenHeight == -1)
			throw new IllegalArgumentException("Screen Lenghts Not Yet Set");
		
		// Gets ratios based on the size of PhysicalWorld
		physicsRatioW = screenWidth / srcWidth;
		physicsRatioH = screenHeight / srcHeight;
		DisplayConverter.srcHeight = srcHeight;
	}
	
	public static Point getDisplaySize() {
		return new Point(screenWidth, screenHeight);
	}
	
	public static Point convertFromPhysics(float x, float y) {
		return new Point(x * physicsRatioW, y * physicsRatioH);
	}
	
	public static Point convertFromDisplay(float x, float y) {
		return convertFromDeviceScreen(x, y, false);
	}
	
	public static Point convertFromDeviceScreen(float x, float y, boolean invertY) {
		if (invertY)
			return new Point(x / physicsRatioW, srcHeight - y / physicsRatioH);
		else
			return new Point(x / physicsRatioW, y / physicsRatioH);
	}
	
	
	/**
	 * Gets the Ratio of the Current screen to the Target Screen Width<br>
	 * Usage: Pixel in Target Screen Width * RatioWidth = Pixel in Current Screen
	 * @return Ratio Width
	 */
	public static float getScreenRatioWidth() {
		return screenRatioW;
	}
	
	/**
	 * Gets the Ratio of the Current screen to the Target Screen Height<br>
	 * Usage: Pixel in Target Screen Height * RatioHeight = Pixel in Current Screen
	 * @return Ratio Height
	 */
	public static float getScreenRatioHeight() {
		return screenRatioH;
	}
}
